﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Supermarket.Dal
{
    public class MeasureDB
    {
        public static void CreateMeasure(Measure measure)
        {
            DataClassesSupermarketDataContext db = new DataClassesSupermarketDataContext();
            
            db.Measures.InsertOnSubmit(measure);
            db.SubmitChanges();
        }

        public static List<Measure> GetAllMeasures()
        {
            DataClassesSupermarketDataContext db = new DataClassesSupermarketDataContext();
            var measures =
                from msr in db.Measures
                select msr;
            return measures.ToList<Measure>();
        }

        public static void Update(Measure measureToUpdate)
        {
            DataClassesSupermarketDataContext db = new DataClassesSupermarketDataContext();
            var measure =
                from msr in db.Measures
                where msr.MeasureId == measureToUpdate.MeasureId
                select msr;

            measure.First().MeasureName = measureToUpdate.MeasureName;
            measure.First().MeasureCode = measureToUpdate.MeasureCode;
            db.SubmitChanges();
        }

        public static void Delete(int id)
        {
            DataClassesSupermarketDataContext db = new DataClassesSupermarketDataContext();
            var deleteMeasure =
                from msr in db.Measures
                where msr.MeasureId == id
                select msr;
            if (deleteMeasure.Count() > 0)
            {
                db.Measures.DeleteOnSubmit(deleteMeasure.First());
                db.SubmitChanges();
            }
        }
    }
}
